<template>
  <div class="page-content">
    <LylTable
      :data-list="dataList"
      v-bind="contentTableConfig"
      :listCount="dataCount"
      v-model:page="pageInfo"
    >
      <template #headerHandler v-if="contentTableConfig.newName">
        <el-button
          type="primary"
          size="default"
          v-if="isCreate"
          @click="handleNewClick"
          >{{ contentTableConfig.newName }}</el-button
        >
      </template>
      <!-- 2.列中的插槽 -->
      <template #status="scope">
        <el-button
          plain
          size="small"
          :type="scope.row.enable ? 'success' : 'danger'"
        >
          {{ scope.row.enable ? '启用' : '禁用' }}
        </el-button>
      </template>
      <template #createAt="scope">
        <span>{{ $filters.formatTime(scope.row.createAt) }}</span>
      </template>
      <template #updateAt="scope">
        <span>{{ $filters.formatTime(scope.row.updateAt) }}</span>
      </template>
      <template #handler="scope">
        <div class="handler-btn">
          <el-button
            size="small"
            type="success"
            text
            @click="handleLookClick(scope.row)"
            ><el-icon><View /></el-icon>查看</el-button
          >
          <el-button
            size="small"
            v-if="isUpdate"
            type="primary"
            text
            @click="handleEditClick(scope.row)"
            ><el-icon><Edit /></el-icon>编辑</el-button
          >
          <el-button
            size="small"
            v-if="isDelete"
            type="danger"
            text
            @click="handleDeleteClick(scope.row)"
            ><el-icon><Delete /></el-icon>删除</el-button
          >
        </div>
      </template>
      <template
        v-for="item in otherPropSlots"
        :key="item.prop"
        #[item.slotName]="scope"
      >
        <template v-if="item.slotName">
          <slot :name="item.slotName" :row="scope.row"></slot>
        </template>
      </template>
    </LylTable>
  </div>
</template>

<script setup lang="ts">
import LylTable from '@/base-ui/table/src/table.vue'
import { View, Edit, Delete } from '@element-plus/icons-vue'
import {
  computed,
  defineProps,
  ref,
  watch,
  defineExpose,
  defineEmits
} from 'vue'
import store from '@/store'
import { usePermission } from '@/hooks/usePermission'
import { ElMessage, ElMessageBox } from 'element-plus'

const props = defineProps({
  contentTableConfig: {
    type: Object,
    required: true
  },
  pageName: {
    type: String,
    required: true
  }
})
// 1.双向绑定pageInfo
const pageInfo = ref({ currentPage: 1, pageSize: 10 })
// 监听分页器的变化
watch(pageInfo, () => getPageData())

/**
 * getPageData 2.发送网络请求
 * @param pageName 当前页标识Name
 * @param queryInfo 携带其他请求数据，拼接上搜索按钮传递过来的搜索查询条件
 */
const getPageData = (queryInfo: any = {}) => {
  store.dispatch('system/getPageListAction', {
    pageName: props.pageName,
    queryInfo: {
      offset: (pageInfo.value.currentPage - 1) * pageInfo.value.pageSize,
      size: pageInfo.value.pageSize,
      ...queryInfo
    }
  })
}
getPageData()

// 3.从vuex中获取数据
const dataList = computed(() =>
  store.getters[`system/pageListData`](props.pageName)
)
const dataCount = computed(() =>
  store.getters[`system/pageListCount`](props.pageName)
)

// 获取其他动态插槽的名称
const otherPropSlots = props.contentTableConfig.propList.filter((item: any) => {
  if (item.slotName === 'status') return false
  if (item.slotName === 'createAt') return false
  if (item.slotName === 'updateAt') return false
  if (item.slotName === 'handler') return false
  return true
})

// 按钮级别的权限
const isCreate = usePermission(props.pageName, 'create')
const isUpdate = usePermission(props.pageName, 'update')
const isDelete = usePermission(props.pageName, 'delete')

const emit = defineEmits(['newBtnClick', 'editBtnClick', 'lookBtnClick'])
// 查看
const handleLookClick = (item: any) => {
  emit('lookBtnClick', item)
}
// 新建
const handleNewClick = () => {
  emit('newBtnClick')
}
// 编辑
const handleEditClick = (item: any) => {
  emit('editBtnClick', item)
}
// 删除
const handleDeleteClick = (item: any) => {
  // 删除确认框
  ElMessageBox.confirm('确定要删除这条数据?', '删除', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      store.dispatch('system/deletePageDataAction', {
        pageName: props.pageName,
        id: item.id
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除'
      })
    })
}
/**
 * 暴露本组件中中的属性方法2，以供父组件使用ref获取得到
 */
defineExpose({ getPageData })
</script>

<style scoped></style>
